package com.jr.controller;

import com.alibaba.fastjson.JSON;
import com.jr.entity.Address;
import com.jr.service.AddressService;
import com.jr.service.impl.AddressServiceImpl;
import com.jr.util.Message;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.ad")
@MultipartConfig
public class AddressController extends HttpServlet {
    private AddressService addressService = new AddressServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1,servletPath.length() - 3);
        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id设置地址默认值
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void setAddressDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        Integer addressId = Integer.valueOf(request.getParameter("addressId"));

        try {
            int i1 = addressService.setAllAddressDefault();
            int i = addressService.setAddressDefaultById(addressId);
            if(i>0){
                out.write(JSON.toJSONString(new Message(530,"设置成功")));
            } else{
                out.write(JSON.toJSONString(new Message(531,"设置失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    /**
     * 根据id更新地址
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateAddressById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        Integer addressId = Integer.valueOf(request.getParameter("id"));
        String addressee = request.getParameter("addressee");
        String phone= request.getParameter("phone");
        String area = request.getParameter("area");
        String address_detail = request.getParameter("address_detail");
        Address address = new Address(addressId,addressee,phone,area,address_detail);
        //System.out.println(address);
        try {
            int i = addressService.updateAddressById(address);
            if(i>0){
                out.write(JSON.toJSONString(new Message(520,"更新成功")));
            } else {
                out.write(JSON.toJSONString(new Message(521,"更新失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    /**
     * 新增地址servlet
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String addressee = request.getParameter("addressee");
       String phone = request.getParameter("phone");
       String area = request.getParameter("area");
       String address_detail = request.getParameter("address_detail");
        PrintWriter out = response.getWriter();
        Address address = new Address();
        Object enterpriseId = request.getSession().getAttribute("enterpriseId");
        address.setEnterprise_id((Integer)enterpriseId);
        address.setAddressee(addressee);
        address.setPhone(phone);
        address.setArea(area);
        address.setAddress_detail(address_detail);
        try {
            int i = addressService.addAddress(address);
            if(i>0){
                out.write(JSON.toJSONString(new Message(500,"新增成功！")));
            }else {
                out.write(JSON.toJSONString(new Message(501,"新增失败！")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 查询所有企业相关的地址
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void listAddress(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {
            List<Address> addresses = addressService.listAllAddress((Integer) request.getSession().getAttribute("enterpriseId"));
            out.write(JSON.toJSONString(new ResultUtil("address",addresses)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 删除地址
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateAddressExist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        int addressId = Integer.parseInt(request.getParameter("addressId"));
        try {
            int i = addressService.updateAddressExist(addressId);
            if (i > 0) {
                out.write(JSON.toJSONString(new Message(510, "删除成功")));
            } else {
                out.write(JSON.toJSONString(new Message(511, "删除失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
